
from PIL import Image, ImageFilter
import time
from PIL import ImageGrab
import pytesseract
import argparse
import cv2
import os

config={
    'baseFile':"C:\\Users\\DingMiaoBin\\Desktop\\资料\\数字图像处理技术"
}
def getFileName(name):
    return(f'C:\\Users\\DingMiaoBin\\Desktop\\资料\\数字图像处理技术\\{name}.png')  # Press Ctrl+F8 to toggle the breakpoint.
def make_screenshot(x1, y1, x2,y2,fileName):
    """截图

    :param x1: 开始截图的x1坐标
    :param y1: 开始截图的x1标
    :param x2: 开始截图的x2坐标
    :param y2: 结束截图的y2坐标
    :return: None
    """
    bbox = (x1, y1, x2,y2)
    im = ImageGrab.grab(bbox)
    im.save(fileName)  # 保存截图文件的路径


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    fileList=[]
    for root,dirs,files in os.walk(config['baseFile']):
        for file in files:
            if os.path.splitext(file)[1]=='.png':
               fileList.append(os.path.join(root,file))
    for fileName in fileList:
        myImage = Image.open(fileName)
        #锐化
        myImage = myImage.convert('RGB').filter(ImageFilter.SHARPEN)
        #转为灰度图
        myImage = myImage.convert('L')
        myImage.show()
        #解析文字
        text = pytesseract.image_to_string(myImage, lang='chi_sim')
        print(fileName)
        print("------------------")
        print(text)